<!-- eslint-disable vue/valid-v-for -->
<template>
  <div v-if="setView === 'list'">
    <ContentWrap>
      <HeaderTitle
        :title="'影响场模型管理'"
        :content="'自定义影响场模型，包括圆模型、椭圆模型、线源模型。'"
        :isdivider="false"
      />
    </ContentWrap>
    <ContentWrap>
      <Search
        :schema="allSchemasList"
        @search="setSearchParams"
        @reset="setSearchParams"
      >
      </Search>
      <div style="margin: 10px 0">
        <el-button type="primary" @click="newAdd" v-hasPermi="['model:influence:create']">
          新增
        </el-button>
      </div>
      <Table
        :columns="allSchemas.tableColumns"
        :data="tableObject.tableList"
        :loading="loading"
        :pagination="{
          total: tableObject.total
        }"
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
        :stripe="true"
        header-cell-class-name="custom-header"
      >
        <template #modelName="{ row }">
          <el-button link type="primary" @click="openForm('View', row)">
            {{ row.modelName }}
          </el-button>
        </template>
        <template #action="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('Edit', row)"
            v-hasPermi="['model:influence:update']"
            v-if="row.flag != '0'"
          >
            修改
          </el-button>

          <el-button
            link
            type="danger"
            @click="handleDelete(row)"
            v-hasPermi="['model:influence:delete']"
            v-if="row.flag != '0'"
          >
            删除
          </el-button>
          <el-button
            link
            type="primary"
            v-if="row.type != '专用'"
            @click="openForm('Copy', row)"
            v-hasPermi="['model:influence:copy']"
          >
            复制
          </el-button>
        </template>
      </Table>
    </ContentWrap>
  </div>

  <TemplateAdd v-if="setView === 'add'" :flag="flag" :formDatas="formData" @setClose="setClose" />
</template>
  <script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import * as templateApi from '@/api/template/index'
import router from '@/router'
import { allSchemas } from './template.data'
import TemplateAdd from './templateAdd.vue'
import { useUserStore } from '@/store/modules/user'
import { checkPermissions } from '@/utils/permissionsPlugin'

const getUserInfo = useUserStore().getUserInfo
// const flag =
//   getUserInfo.roles.some(
//     (item) =>
//       item.name == '超级管理员' || item.name == '国家级管理员' || item.name == '国家级业务人员'
//   ) && checkPermissions(['model:influence:select'])
  const flag =
  getUserInfo.roles.some(
    (item) =>
      item.name == '超级管理员' || item.name == '国家级管理员' || item.name == '国家级业务人员')
const allSchemasList = computed(() => {
  if (flag) {
    return allSchemas.searchSchema
  } else {
    return allSchemas.searchSchema.slice(1)
  }
})
console.log(allSchemas.searchSchema,'===');

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(false) // 列表的加载中

const { tableObject, register, tableMethods } = useTable({
  getListApi: templateApi.getModelPage, // 分页接口
  delListApi: templateApi.deleteModel, // 删除接口
  defaultParams: {} //传递默认数据
})
// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods
const formData = ref({
  id: '',
  type: '',
  dtype: '',
  divisionId: ''
}) // 搜索的表单

const setView = ref('list')
const setViewFun = (type: string) => {
  setView.value = type
}
const setClose = () => {
  setView.value = 'list'
  getList()
}

const handleDelete = async (row) => {
  // 批量删除 获取id数组
  try {
    //确认删除
    await message.delConfirm()
    await templateApi.deleteModel({ modelId: row.id, type: row.type })
    // message.success(data)
    // 刷新列表
    await getList()
  } catch {}
}

/** 添加/修改操作 */
const newAdd = () => {
  setViewFun('add')
  formData.value.id = ''
  formData.value.type = ''
  formData.value.dtype = ''
  formData.value.divisionId = ''

  // router.push({
  //     path: '/quake/report/templateAdd',
  // })
}
const openForm = (type: string, row) => {
  formData.value.id = row.id
  formData.value.type = type
  formData.value.dtype = row.type
  formData.value.divisionId = row.divisionId
  setViewFun('add')
}

/** 初始化 */
onMounted(() => {
  getList()
})
</script>
 <style lang="scss" scoped>
/* 使用::v-deep调整每一行的样式 */
::deep(.el-table) {
  .el-table__row {
    font-size: 16px;
    height: 40px;
    /* 设置每一行的高度 */
    line-height: 40px;
    /* 设置行内文本的垂直对齐 */
    color: #333333;
  }
}
.titleFont {
  display: inline-block;
  font-size: 16px;
  color: #333;
  font-weight: bold;
  margin-bottom: 15px;
}

:deep(.custom-header) {
  background-color: #f2f3f5 !important;
  /* 设置表头背景色 */
  color: #333 !important;
  /* 设置表头文字颜色 */
  font-weight: bold !important;
  /* 设置表头文字加粗 */
  height: 42px;
}
</style> 